package com.example.vm.schedul;

import cn.hutool.core.collection.CollectionUtil;
import com.example.vm.domain.vo.HotVideo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;

public class TopK {
    int k=0;
    private Queue<HotVideo> hotQueue;

    public TopK(int k, Queue<HotVideo> hotQueue){
        this.k=k;
        this.hotQueue=hotQueue;
    }
    public void add(HotVideo hotVideo){
        if(hotQueue.size()<k){
            hotQueue.add(hotVideo);
        }else if (hotQueue.peek().getHot()<hotVideo.getHot()){
            hotQueue.poll();
            hotQueue.add(hotVideo);
        }
        return;
    }
    public List<HotVideo> get(){
        List<HotVideo> hotVideoList = new ArrayList<>(hotQueue.size());
        while(!hotQueue.isEmpty()){
            hotVideoList.add(hotQueue.poll());
        }
        Collections.reverse(hotVideoList);
        return hotVideoList;
    }
}
